// pages/vote/index.js
const http = require('../../utils/http.js')
const config = require('../../config.js')
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrls: [],    //参赛人图片
    autoplay: true, //是否自动切换
    interval: 5000, //自动切换时间间隔
    duration: 1000, //滑动动画时长
    manId:'',   //参赛人ID
    name: '',   //参赛人姓名
    content: '',  //参赛人简介  
    ranking: '',  //当前排名
    ticket: '',   //当前票数
    vote: '',   //用户是否为当前参赛人在规定时间内投过票
    loading: false,   //按钮是否显示点击状态
    disabled: false,  //按钮是否禁用
    circular: true, //是否采用衔接滑动
    parentId: 1,  //分享人ID
    title: '',  
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options.manId)
    var that = this
    this.setData({
      manId: options.manId,
    })
    if (options.hasOwnProperty('parentId')) {
      that.setData({
        parentId: options.parentId
      })
    }
    wx.setStorageSync("parentId", that.data.parentId)
  },
  /**
  * 生命周期函数--监听页面显示
  */
  onShow: function () {
    console.log(this.data.manId)
    this.getManInfo()
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  //获取参赛者详情
  getManInfo: function(){
    var that = this
    var data = {

    }
    var manInfoUrl = config.manInfo + '/' + that.data.manId
    http.httpReq(manInfoUrl,"GET",data,function(res){
      console.log(res)
      var imgUrls = app.imgArr(res.data.imgs)
      that.setData({
        imgUrls,
        name: res.data.name,
        content: res.data.content,
        ranking: res.data.ranking,
        ticket: res.data.ticket,
        vote: res.data.vote,
        parentId: res.data.loginId,
        title: res.data.title
      })
      wx.setStorageSync("parentId", that.data.parentId)
    })
  },
  /**
   * 投票
   */
  voteManTap: function(){
    var that = this
    if (that.data.vote) {
      wx.showToast({
        title: '每人一天只能投一票哦',
        icon: 'none'
      })
      return
    }
    that.setData({
      loading: true,
      disabled: true
    })
    var data = {
      playerId: that.data.manId
    } 
    http.httpReq(config.voteMan,"POST",data,function(res){
      that.setData({
        ranking: res.data.ranking,
        ticket: res.data.ticket,
        vote: res.data.vote,
        loading: false,
        disabled: false
      })
      wx.showToast({
        title: '投票成功',
        icon: 'success'
      })
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var that = this
    console.log(that.data.manId)
    console.log(that.data.parentId)
    return {
      title: that.data.title,
      desc: '来自' + that.data.name + '的分享',
      path: "/pages/vote/index?manId=" + that.data.manId + "&parentId=" + that.data.parentId, // 路径，传递参数到指定页面。
    }
  },
  /**
   * 去首页
   */
  goHomeTap: function () {
    wx.reLaunch({
      url: "/pages/activity/index"
    })
  }
})